package com.yc.testupload.crawler.task;

import java.util.HashMap;
import java.util.Map;

/**
 * 抓取任务类
 * 表示一个具体的抓取任务，包含网站信息、频道信息、分类ID和抓取正则表达式等
 */
public class CrawlTask {
    private String websiteId; // 网站ID
    private String websiteName; // 网站名称
    private String websiteDomain; // 网站域名
    private String channelId; // 频道ID
    private String channelName; // 频道名称
    private int categoryId; // 对应的文库分类ID
    private Map<String, String> regexPatterns; // 抓取相关的正则表达式配置
    private boolean enabled; // 任务是否启用
    private int priority; // 任务优先级，数字越小优先级越高
    private String description; // 任务描述

    // 构造函数
    public CrawlTask(String websiteId, String websiteName, String websiteDomain, String channelId, 
                    String channelName, int categoryId, Map<String, String> regexPatterns) {
        this.websiteId = websiteId;
        this.websiteName = websiteName;
        this.websiteDomain = websiteDomain;
        this.channelId = channelId;
        this.channelName = channelName;
        this.categoryId = categoryId;
        this.regexPatterns = new HashMap<>(regexPatterns != null ? regexPatterns : new HashMap<>());
        this.enabled = true; // 默认启用
        this.priority = 100; // 默认优先级
        this.description = String.format("抓取%s的%s频道内容到文库分类ID: %d", 
                websiteName, channelName, categoryId);
    }

    // getter和setter
    public String getWebsiteId() {
        return websiteId;
    }

    public void setWebsiteId(String websiteId) {
        this.websiteId = websiteId;
    }

    public String getWebsiteName() {
        return websiteName;
    }

    public void setWebsiteName(String websiteName) {
        this.websiteName = websiteName;
    }

    public String getWebsiteDomain() {
        return websiteDomain;
    }

    public void setWebsiteDomain(String websiteDomain) {
        this.websiteDomain = websiteDomain;
    }

    public String getChannelId() {
        return channelId;
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    public Map<String, String> getRegexPatterns() {
        return new HashMap<>(regexPatterns);
    }

    public void setRegexPatterns(Map<String, String> regexPatterns) {
        this.regexPatterns = new HashMap<>(regexPatterns != null ? regexPatterns : new HashMap<>());
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * 根据名称获取正则表达式模式
     */
    public String getRegexPattern(String patternName) {
        return regexPatterns.get(patternName);
    }

    /**
     * 设置正则表达式模式
     */
    public void setRegexPattern(String patternName, String pattern) {
        regexPatterns.put(patternName, pattern);
    }

    /**
     * 检查URL是否匹配当前任务的网站
     */
    public boolean matchesUrl(String url) {
        // 简单实现：检查URL是否包含网站域名
        return url.contains(websiteDomain);
    }

    @Override
    public String toString() {
        return "CrawlTask{" +
                "websiteId='" + websiteId + '\'' +
                ", websiteName='" + websiteName + '\'' +
                ", channelId='" + channelId + '\'' +
                ", channelName='" + channelName + '\'' +
                ", categoryId=" + categoryId +
                ", enabled=" + enabled +
                ", priority=" + priority +
                '}';
    }
}